{% extends "stocksys/base/base_page.html" %}
{% load staticfiles %}
{% block headscript %}
    <script>
        function delete_rule(rule_id) {
            $.ajax({
                url: "{% url 'stocksys:delete_rules' %}",
                dataType: "json",
                method: "DELETE",
                headers: {'X-CSRFToken': $.cookie('csrftoken')},
                data: {
                    rule_id: rule_id
                },
                success: function (data) {
                    if (data['state'] === 200) {
                        swal({
                            title: '删除成功',
                            text: data['info'],
                            confirmButtonClass: 'btn btn-success',
                            buttonsStyling: false,
                            type: "success"
                        }).catch(swal.noop);
                    } else {
                        swal({
                            title: "删除失败",
                            text: data['info'],
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-error",
                            type: "error"
                        }).catch(swal.noop);
                    }
                }
            });
        }
    </script>
{% endblock %}
{% block content %}
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="content">
                    <div class="btn-toolbar" role="toolbar">
                        <div class="btn-group btn-group-seperator">
                            <button type="button" id="new_rule" class="btn btn-default "><i
                                    class="ti-save">新建规则</i></button>
                        </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="card">
                        <div class="content">
                            <h4 class="title">规则列表</h4>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead class="text-primary">
                                    <tr>
                                        <th>规则ID</th>
                                        <th>规则名称</th>
                                        <th>上传时间</th>
                                        <th>回测日期</th>
                                        <th>当前规则使用模型</th>
                                        <th>最大回测收益率</th>
                                        <th>平均回测收益率</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for rule in  rule_info %}
                                        <tr>
                                            <td>{{ rule.id }}</td>
                                            <td>{{ rule.name }}</td>
                                            <td>{{ rule.create_datetime | date:"Y-m-d" }}</td>
                                            <td>{{ rule.update_datetime | date:"Y-m-d" }}</td>
                                            {% ifequal rule.model_name -1 %}
                                                <td>无</td>
                                            {% endifequal %}
                                            {% ifnotequal rule.model_name -1 %}
                                                <td>{{ rule.model_name }}</td>
                                            {% endifnotequal %}
                                            <td>{{ rule.max_back_measurable }}</td>
                                            <td>{{ rule.mean_back_measurable }}</td>
                                            <td class="td-actions text-right">
                                                <a href="{% url 'stocksys:rule_edit' id=rule.id %}"
                                                   rel="tooltip" title="详细信息"
                                                   class="btn btn-success btn-simple btn-xs">
                                                    <i class="ti-info"></i>
                                                </a>
                                                <p onclick="delete_rule({{ rule.id }})" rel="tooltip" title="删除"
                                                   class="btn btn-danger btn-simple btn-xs">
                                                    <i class="ti-close"></i>
                                                </p>
                                            </td>
                                        </tr>
                                    {% endfor %}

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block pagescript %}
    <script>
        $("#new_rule").bind("click", function () {
            swal({
                title: '新建规则',
                html: '<div class="form-group">' +
                    '<input id="input-field" type="text" placeholder="规则名称" class="form-control" />' +
                    '<input id="input-save-field" type="text" placeholder="规则文件名" class="form-control" />' +
                    '</div>',
                showCancelButton: true,
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                buttonsStyling: false
            }).then(function (result) {
                var name = $('#input-field').val();
                var name_en = $('#input-save-field').val();
                $.ajax({
                    url: "{% url 'stocksys:save_rules' %}",
                    dataType: "json",
                    method: "POST",
                    headers: {'X-CSRFToken': $.cookie('csrftoken')},
                    data: {
                        name: name,
                        name_en: name_en
                    },
                    success: function (data) {
                        if (data['state'] === 200) {
                            swal({
                                title: '创建成功',
                                text: data['info'],
                                confirmButtonClass: 'btn btn-success',
                                buttonsStyling: false,
                                type: "success"
                            }).catch(swal.noop);
                        } else {
                            swal({
                                title: '删除失败',
                                text: data['info'],
                                confirmButtonClass: 'btn btn-danger',
                                buttonsStyling: false,
                                type: "error"
                            }).catch(swal.noop);
                        }
                    }
                });
            }).catch(swal.noop)
        })
    </script>
{% endblock %}

